<?php
namespace Rainrock\Framework\Controller;


use Rainrock\Framework\kernel\core\Controller;
use Rainrock\Framework\kernel\core\Module;
use Rainrock\Framework\kernel\base\Request;
use Rainrock\Framework\kernel\base\Base;
use Rainrock\Framework\kernel\base\File;
use Rainrock\Framework\kernel\base\Web;
use Rainrock\Framework\kernel\base\Session;
use Rainrock\Framework\kernel\Html\HtmlBase;
use Rainrock\Framework\kernel\Html\HtmlLabel;
use Rainrock\Framework\kernel\Html\HtmlTable;
use Rainrock\Framework\kernel\Html\HtmlInput;
use Rainrock\Framework\kernel\Html\HtmlButton;
use Rainrock\Framework\kernel\Html\HtmlImage;

/**
*	数据库管理
*/
class DbmanageController extends Controller{
	
	
	public function checkAccess($act='')
	{
		if($act=='check' || $act=='exit')return;
		$id = Session::getint('dbmanage');
		if($id >0)return;

		$html = HtmlBase::get();
		$html->setTitle(TITLE);
		$html->addJse('dbmanagelogin');
		
		$div1 = HtmlLabel::get('h3')
			->addStyle('padding','10px')
			->addCont('模块管理登录');

		$div2 = HtmlLabel::get()
			->addStyle('padding','10px')
			->setDisplay('flex')
			->addCont(
				HtmlInput::gets()
				->setTishi('请输入管理密码')
				->setType('password')
				->addStyle('flex',1)
				->setLen(50)
				->setId('pass')
			);
			
		$div3 = HtmlLabel::get()
			->addStyle('padding','10px')
			->addCont(
				HtmlButton::gets()
				->setValue('登录')
				->setWidth('100px')
				->setonClick('submitlogin(this)')
			)
			->addKong()
			->addCont(HtmlLabel::get('span')->setId('msgview'));
				
		$mdivs = HtmlLabel::get()->setAlign('left')
				->addCont($div1)
				->addCont($div2)
				->addCont($div3)
				->setWidth('300px')
				->addCss('border')
				->setBackgroundColor('var(--main-bgcolor)')
				->setRadius(5)
				->addStyle('margin-top','20px')
				->addStyle('padding','20px');
		
		$html->addBody(HtmlLabel::get()->setHeight(30,'hidden'));
		$logo = ''.ROOT_PATH.'/images/logo.png';
		if(file_exists($logo)){
			$html->addBody(HtmlLabel::get()->addCont(HtmlImage::gets()->setSrc(''.URL.'images/logo.png')->setWidth(100)->setHeight(100,'hidden')->setTitle(TITLE)->setRadius('50%')->setonClick('js.reload()'))->addStyle('margin-top','15px'));
		}
		$html->addBody($mdivs);
		
		$html->addBody(
			HtmlLabel::get()->setFontsize(13)->addCss('hui')
			->addCont('Copyright ©'.date('Y').' '.TITLE.'v'.VERSION.' - 版权所有：')
			->addCont(HtmlLabel::get('a')->setFontsize(13)->addCont('信呼开发团队')->setIsnew(true)->setHref(URLY))
			->addStyle('margin','15px')
		);

		return $html->show();
	}
	
	
	/**
	*	管理登录
	*/
	public function checkAction()
	{
		$pass 		= Request::get('pass');
		if($this->isempt($pass))return $this->returnerror('error pass');
		$highpass = Base::getConfig('highpass');
		if($this->isempt($highpass))return $this->returnerror('未设置管理密码');
		if(md5($highpass) != $pass)return $this->returnerror('管理密码错误');
		Session::set('dbmanage', '1');
		return $this->returnsuccess();
	}
	
	
	/**
	*	首页
	*/
	public function indexAction()
	{
		$html = new HtmlBase();
		$html->setTitle('模块管理_'.TITLE.'');

		$mdiv2 = $this->getmDivs();
		
		$html->addBody(HtmlLabel::get('h4')->addStyle('margin-top','20px')->addCont('模块管理'));
		//$html->addBody($mdivs);
		
		$html->addBody($mdiv2);
		$html->addBody(
			HtmlLabel::get('a')
			->addStyle('margin','20px')
			->setDisplay('block')
			->addStyle('text-align','center')
			->setWidth('150px')
			->addCss('webbtn')
			->setHref('?m=dbmanage&a=exit')
			->setBackgroundColor('#D9534F')
			->addCont('退出管理')
		);
		return $html->show();
	}
	
	/**
	*	退出管理
	*/
	public function exitAction()
	{
		Session::remove('dbmanage');
		
		return MsgView::Success('退出成功','进入主页','?m=index');
	}
	
	
	/**
	*	表格管理（弃用）
	*/
	public function tableAction()
	{
		return 'error';
		$tab 		= Request::get('table');
		if(!$tab)return '没有选择表';
		
		$page 		= Request::getint('page','1');
		$db 	  	= $this->getDb();
		$farr		= $db->getAllfields($tab);	
		$limit		= 10;
		$html = new HtmlBase();
		$html->setTitle('表('.$tab.')');
		$html->addCss('table');
		$data 		= $db->getall($tab, '1=1','*','`id` desc', $limit, $page);
		$count 		= $db->getCount($tab, '1=1');
		$htable 	= HtmlTable::gets();
		$htable->setHeader($farr);
		$htable->setData($data);
		
		$html->addBody(
			HtmlLabel::get('h4')
			->addStyle('padding','0px 10px')
			->addStyle('margin-top','10px')
			->addCont('表('.$tab.')')
		);
		
		$html->addBody(
			HtmlLabel::get()
			->addStyle('padding','10px')
			->addCont($htable)
		);
		
		$jldiv = HtmlLabel::get()
			->addStyle('padding','0px 10px')
			->addCont('共记录'.$count.'条');
		
		$max = ceil($count / $limit);
		
		if($page > 1){
			$jldiv->addKong();
			$jldiv->addCont(HtmlLabel::get('a')->setHref('?m=dbmanage&a=table&table='.$tab.'&page='.($page-1).'')->addCont('上一页'));
		}
		
		if($page < $max){
			$jldiv->addKong();
			$jldiv->addCont(HtmlLabel::get('a')->setHref('?m=dbmanage&a=table&table='.$tab.'&page='.($page+1).'')->addCont('下一页'));
		}
		
		$jldiv->addKong();
		$jldiv->addCont('共'.$max.'页');
		$jldiv->addBlank(10);
		
		$html->addBody($jldiv);
		$html->setAlign('left');
		return $html->show();
	}
	
	
	/**
	*	PC主页空间
	*/
	public function mainAction()
	{
		
	}
	
	private function getmDivs()
	{
		$mdiv2 = HtmlLabel::get()->setAlign('left')
				->setWidth('300px')
				->addCss('border')
				->setBackgroundColor('var(--main-bgcolor)')
				->setRadius(5)
				->addStyle('margin-top','20px')
				->addStyle('padding','10px');
		
		$mdiv2->addCont(HtmlLabel::get('h4')->addCont('模块列表(点击可更新)'));
		
		$groups		= Module::getList();
		$mdiv2->addCont(HtmlLabel::get()
			->addCont(
				HtmlLabel::get('a')
				->setHref('?m=database&a=update&num=all')
				->setIsnew(true)
				->addCont('全部更新')
			)
			->addKong()
			->addCont(
				HtmlLabel::get('a')
				->setHref('?m=webstatic&a=update')
				->setIsnew(true)
				->addCont('更新前端文件(必须点)')
			)
		);
		
		foreach($groups as $gs=>$als){
			$mdiv2->addCont(HtmlLabel::get()->addCont(
				HtmlLabel::get('a')
				->setHref('?m=database&a=update&group='.$gs.'')
				->setIsnew(true)
				->addCont($gs)
			));
			foreach($als as $nu1){
				$info = Module::get($nu1, $gs);
				if($info){
					$mdiv2->addCont(HtmlLabel::get()
						->addStyle('padding-left','20px')
						->addCont(
							HtmlLabel::get('a')
							->setHref('?m=database&a=update&num='.$nu1.'&zhu='.$gs.'')
							->setIsnew(true)
							->addCont($info->getName().'('.$nu1.')')
						)
					);
				}
			}
		}
		return $mdiv2;
	}
}